//index.js
//获取应用实例
var app = getApp();
var config = require('../../../../config.js');
Page({
  data: {
    showModal: false,
    fromType: 2,
    times:0,
    isShare:0
  },

  // 跳转至答题
  toGamedetails: function (e) {
    var that=this;
    wx.navigateTo({
      url: '../loading/loading?fromType=2&times=' + that.data.times + '&isShare=' + that.data.isShare
    })

  },
 /**
   * 判断用户是否有机会游戏
   */
  IsCanPlay:function(e){
    var that = this;
   wx:wx.request({
     url: config.isCanPlay,
     data: {
       openId: app.globalData.openid
     },
     method: 'POST',
     success: function(res) {
       console.log(config.isCanPlay)
       console.log(JSON.stringify(res.data))
       /**
       * 1=有  0=无
       */
       that.setData({
         times:res.data.rows.remainTimes,
         isShare:res.data.rows.haveShared
       })
       console.log(res.data.rows)
       if (res.data.rows.remainTimes >= 1){
         that.toGamedetails();
      }else{
         wx.showModal({
           title: '提示',
           content: '您今天的闯关机会已经用完咯！',
           success: function (res) {
             if (res.confirm) {
               console.log('用户点击确定')
             } else if (res.cancel) {
               console.log('用户点击取消')
             }  
           }
         })
      }
      return;
     },
    
   })

  },
   


 /**
   * 页面加载完成
   */

  onLoad: function (options) {
    // app.globalData.bgMusic.stop();
    // app.globalData.bgMusicF.play();
    this.getGateList();
  },
  /**
     * 每次打开页面都会调用一次。
     */
  onShow: function () {
    // app.globalData.bgMusicF.play();
  },
  /**
    * 生命周期函数--监听页面卸载
    */
  onUnload: function () {
    // app.globalData.bgMusic.play();
    // app.globalData.bgMusicF.stop();
  },
  /**
   * 获取关卡集合
   */
  getGateList: function() {
    var that = this;
    wx.request({
      url: config.getGameLevels,
      method: 'POST',
      data: {
        openId: wx.getStorageSync('sessionId')
      },
      success: function (res) {
        // console.log(JSON.stringify(res.data));
        if (res.data.success == 0) {
            that.setData({
              levelsList: res.data.rows.miniGateAlone,
              rules: res.data.rows.miniPlays.introduce,
              // maxLevel: res.data.rows.miniUserGate.maxGate
        
            });

             console.log(that.data.maxLevel); 
            //在缓存中存储数据 通关时调用

            console.log(res.data.rows); 
            wx.setStorageSync(config.gateList, res.data.rows);
            console.log('11',wx.getStorageSync(config.gateList))




        }
      },
      fail: function(){


      }
      
    });

  },



  /**
  * 邀请好友
  */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    wx: wx.request({
        url: config.getOneChance,
        data: {
          openId: app.globalData.openid
        },
        method: 'POST',
        success: function (res) {
           
        },
    })
    return {
      title: app.globalData.userInfo.nickName +'邀请你答题冲榜！',
      path: '/pages/index/index?pageId=' + config.gameInstruction,
      success: function (res) {
       

        
      }
    }
  },
  /**
     * 弹窗
     */
  showDialogBtn: function () {
    this.setData({
      showModal: true
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {
  },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.hideModal();
  }
})

